<template>
  <div :style="{ ...coreStyle.marginSame(8) }">
    <a-card>
      <a-form ref="sqlStrFormQueryRef" :model="sqlStrFormQueryModel" labelAlign="left" layout="vertical" :style="{ ...coreStyle.marginBottom(10) }">
        <a-row :gutter="16" v-show="!sqlStrFormQueryVisible">
          <a-col :span="8">
            <a-form-item :style="{ ...coreStyle.marginBottom(-8) }">
              <a-input-search v-model:value="sqlStrFormQueryModel.keySearchVo" placeholder="关键字搜索" :disabled="sqlStrFormQuerySubmitting" @search="sqlStrFormQuerySearch" />
            </a-form-item>
          </a-col>
          <a-col :span="16">
            <a-button type="link" :style="{ ...coreStyle.floatRight }" @click="sqlStrFormQueryUpDown">
              展开
              <CoreIcon :icon="coreStyle.antBtnIconExpand" :size="14" />
            </a-button>
          </a-col>
        </a-row>
        <a-row :gutter="16" v-show="sqlStrFormQueryVisible">
          <a-col :span="8">
            <a-form-item>
              <a-input-search v-model:value="sqlStrFormQueryModel.keySearchVo" placeholder="关键字搜索" :disabled="sqlStrFormQuerySubmitting" @search="sqlStrFormQuerySearch" />
            </a-form-item>
          </a-col>
          <a-col :span="16">
          </a-col>
          <a-col :span="8">
            <a-form-item label="名称">
              <a-input v-model:value="sqlStrFormQueryModel.sqlNameAndLike" placeholder="输入名称" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="分类">
              <a-input v-model:value="sqlStrFormQueryModel.sqlTypeAndLike" placeholder="输入分类" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="sql">
              <a-input v-model:value="sqlStrFormQueryModel.sqlStrAndLike" placeholder="输入sql" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="描述">
              <a-input v-model:value="sqlStrFormQueryModel.descriptionAndLike" placeholder="输入描述" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="排序权重">
              <a-input-number v-model:value="sqlStrFormQueryModel.weightOrder" :style="{ ...coreStyle.width('100%') }" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-space :style="{ ...coreStyle.floatRight }">
              <a-button type="dashed" :style="{ ...coreStyle.antBtnReset }" @click="sqlStrFormQueryReset">
                <CoreIcon :icon="coreStyle.antBtnIconReset" :size="14" />
                重置
              </a-button>
              <a-button :loading="sqlStrFormQuerySubmitting" type="dashed" :style="{ ...coreStyle.antBtnQuery }" @click="sqlStrFormQuerySearch">
                <CoreIcon :icon="coreStyle.antBtnIconQuery" :size="14" />
                查询
              </a-button>
              <a-button type="dashed" ghost :style="{ ...coreStyle.antBtnRetract }" @click="sqlStrFormQueryUpDown">
                收起
                <CoreIcon :icon="coreStyle.antBtnIconRetract" :size="14" />
              </a-button>
            </a-space>
          </a-col>
        </a-row>
      </a-form>
      <a-space>
        <a-button type="dashed" :style="{ ...coreStyle.antBtnAdd }" @click="sqlStrAdd">
          <CoreIcon :icon="coreStyle.antBtnIconAdd" :size="14" />
          新增
        </a-button>
        <a-button v-if="sqlStrListSelectLength === 1" type="dashed" :style="{ ...coreStyle.antBtnUpdate }" @click="sqlStrUpdate">
          <CoreIcon :icon="coreStyle.antBtnIconUpdate" :size="14" />
          修改
        </a-button>
        <a-popconfirm v-if="sqlStrListSelectLength > 0" title="确认删除？" okType="dashed" placement="bottom" @confirm="sqlStrDelete">
          <a-button type="dashed" :style="{ ...coreStyle.antBtnDelete }">
            <CoreIcon :icon="coreStyle.antBtnIconDelete" :size="14" />
            删除
          </a-button>
        </a-popconfirm>
        <a-button v-if="sqlStrListSelectLength === 1" type="dashed" :style="{ ...coreStyle.antBtnDetail }" @click="sqlStrDetailInfo">
          <CoreIcon :icon="coreStyle.antBtnIconDetail" :size="14" />
          详情
        </a-button>
      </a-space>
      <a-space :style="{ ...coreStyle.floatRight }">
        <a-button type="dashed" :style="{ ...coreStyle.antBtnTemplateDownload }" @click="sqlStrTemplateDownload">
          <CoreIcon :icon="coreStyle.antBtnIconTemplateDownload" :size="14" />
          模版下载
        </a-button>
        <a-button type="dashed" :style="{ ...coreStyle.antBtnImport }" @click="sqlStrDataImport">
          <CoreIcon :icon="coreStyle.antBtnIconImport" :size="14" />
          导入
        </a-button>
        <a-button type="dashed" :style="{ ...coreStyle.antBtnExport }" @click="sqlStrDataExport">
          <CoreIcon :icon="coreStyle.antBtnIconExport" :size="14" />
          导出
        </a-button>
        <a-button type="dashed" ghost shape="circle" :style="{ ...coreStyle.antBtnRefresh }" @click="sqlStrListGetDataSet">
          <CoreIcon :icon="coreStyle.antBtnIconRefresh" :size="22" />
        </a-button>
      </a-space>
      <a-table
        rowKey="id"
        :columns="sqlStrListColumns"
        :data-source="sqlStrListDataSet"
        :loading="sqlStrListLoading"
        :pagination="false"
        bordered
        :customRow="sqlStrListCustomRow"
        :row-selection="{ fixed: true, selectedRowKeys: sqlStrListSelectedRowKeys, onChange: sqlStrListRowOnChange }"
        :scroll="{ x: '100%', y: 550 }"
        :style="{ ...coreStyle.marginTop(10) }"
      >
      </a-table>
      <a-pagination
        v-model:current="sqlStrListDataPage"
        v-model:pageSize="sqlStrListDataRows"
        :total="sqlStrListDataCount"
        show-size-changer
        show-quick-jumper
        :page-size-options="[ ...coreStyle.antdPaginationPageSizeOptions ]"
        :show-total="(total, range) => coreStyle.antdPaginationShowTotal(total, range)"
        :style="{ ...coreStyle.marginTop(10), ...coreStyle.textAlignCenter }"
        @change="sqlStrListPaginationChange"
        @showSizeChange="sqlStrListPaginationShowSizeChange"
        v-if="sqlStrListPaginationVisible"
      >
      </a-pagination>
      <p :style="{ ...coreStyle.marginTop(14), ...coreStyle.marginBottom(6), ...coreStyle.textAlignCenter }" v-if="!sqlStrListPaginationVisible">
        {{ coreStyle.antdPaginationShowTotal(sqlStrListDataCount) }}
      </p>
    </a-card>
    <a-drawer
      v-model:visible="sqlStrEditDrawerVisible"
      title="编辑sql语句信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
      @close="sqlStrEditDrawerOnClose"
    >
      <a-form ref="sqlStrFormRef" :model="sqlStrFormModel" :rules="sqlStrFormRules" labelAlign="left" layout="vertical">
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item label="名称" name="sqlName">
              <a-input v-model:value="sqlStrFormModel.sqlName" placeholder="输入名称" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="分类" name="sqlType">
              <a-input v-model:value="sqlStrFormModel.sqlType" placeholder="输入分类" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="sql" name="sqlStr">
              <a-textarea v-model:value="sqlStrFormModel.sqlStr" :rows="3" placeholder="输入sql" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="描述" name="description">
              <a-input v-model:value="sqlStrFormModel.description" placeholder="输入描述" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="排序权重" name="weightOrder">
              <a-input-number v-model:value="sqlStrFormModel.weightOrder" :style="{ ...coreStyle.width('100%') }" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button type="dashed" :style="{ ...coreStyle.antBtnReset }" @click="sqlStrFormReset">
            <CoreIcon :icon="coreStyle.antBtnIconReset" :size="14" />
            重置
          </a-button>
          <a-button :loading="sqlStrFormSaveSubmitting" type="dashed" :style="{ ...coreStyle.antBtnNextAdd }" @click="sqlStrFormNextAdd">
            <CoreIcon :icon="coreStyle.antBtnIconNextAdd" :size="14" />
            继续录入
          </a-button>
          <a-button :loading="sqlStrFormSaveSubmitting" type="dashed" :style="{ ...coreStyle.antBtnSave }" @click="sqlStrFormSave">
            <CoreIcon :icon="coreStyle.antBtnIconSave" :size="14" />
            保存
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="sqlStrDetailDrawerVisible"
      title="sql语句详细信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
    >
      <a-space direction="vertical" :size="20" :style="{ ...coreStyle.width('100%') }">
        <a-descriptions title="基础信息" :column="2" bordered>
          <a-descriptions-item label="名称" :span="1">{{ sqlStrDetailModel.sqlName }}</a-descriptions-item>
          <a-descriptions-item label="分类" :span="1">{{ sqlStrDetailModel.sqlType }}</a-descriptions-item>
          <a-descriptions-item label="sql" :span="1">{{ sqlStrDetailModel.sqlStr }}</a-descriptions-item>
          <a-descriptions-item label="描述" :span="1">{{ sqlStrDetailModel.description }}</a-descriptions-item>
          <a-descriptions-item label="排序权重" :span="1">{{ sqlStrDetailModel.weightOrder }}</a-descriptions-item>
        </a-descriptions>
      </a-space>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button type="dashed" :style="{ ...coreStyle.antBtnUpdate }" @click="sqlStrDetailUpdate">
            <CoreIcon :icon="coreStyle.antBtnIconUpdate" :size="14" />
            修改
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="sqlStrDataImportDrawerVisible"
      title="sql语句数据导入"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 50, 0) }"
    >
      <a-row :gutter="16">
        <a-col :span="24">
          <FileUpload ref="sqlStrDataImportFileUploadRef" :fileModule="sqlStrDataImportFileModule" @change="sqlStrDataImportFileUploadChange" />
          <a-input v-model:value="sqlStrDataImportFileUrl" disabled addonBefore="导入文件url:" placeholder="输入导入文件url">
            <template #addonAfter>
              <a-button type="link" size="small" @click="sqlStrDataImportFileUrlSelectFileClick">选择文件</a-button>
            </template>
          </a-input>
        </a-col>
      </a-row>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button :loading="sqlStrDataImportSaveSubmitting" type="dashed" :style="{ ...coreStyle.antBtnImportData }" @click="sqlStrToDataImport">
            <CoreIcon :icon="coreStyle.antBtnIconImportData" :size="14" />
            导入数据
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="sqlStrDataImportFileSelectDrawerVisible"
      title="文件选择"
      placement="right"
      :closable="false"
      destroyOnClose
      :width="'80%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 0, 0) }"
    >
      <FileSelect :fileModule="sqlStrDataImportFileModule" @change="sqlStrDataImportFileSelectChange" />
    </a-drawer>
    <a-drawer
      v-model:visible="sqlStrDataExportDrawerVisible"
      title="sql语句数据导出"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 50, 0) }"
    >
      <DataExportColumnSelect :optionalDataExportFieldList="sqlStrOptionalDataExportFieldList" :selectedDataExportFieldList="sqlStrSelectedDataExportFieldList" />
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button type="dashed" :style="{ ...coreStyle.antBtnQueryDataExport }" @click="sqlStrToDataExport(false)">
            <CoreIcon :icon="coreStyle.antBtnIconQueryDataExport" :size="14" />
            查询数据导出
          </a-button>
          <a-button type="dashed" :style="{ ...coreStyle.antBtnAllDataExport }" @click="sqlStrToDataExport(true)">
            <CoreIcon :icon="coreStyle.antBtnIconAllDataExport" :size="14" />
            全部数据导出
          </a-button>
        </a-space>
      </div>
    </a-drawer>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component'
import { mapStores } from 'pinia'
import { useCoreToolStore } from '/@/store/modules/cn/core/tool/coreToolStore'
import type { Res } from '/@/tool/Res'
import type { Param } from '/@/tool/Param'

import type { DataExportFieldAO } from '/@/model/cn/core/tool/ao/DataExportFieldAO'
import type { DataExportAO } from '/@/model/cn/core/tool/ao/DataExportAO'
import { toDataExportAO } from '/@/model/cn/core/tool/ao/DataExportAO'

import { useSqlStrStore } from '/@/store/modules/cn/core/kpis/sqlStrStore'

import type { SqlStr } from '/@/model/cn/core/kpis/eo/SqlStr'
import { toSqlStr } from '/@/model/cn/core/kpis/eo/SqlStr'

import type { SqlStrQO } from '/@/model/cn/core/kpis/qo/SqlStrQO'
import { toSqlStrQO } from '/@/model/cn/core/kpis/qo/SqlStrQO'

import type { SqlStrVO } from '/@/model/cn/core/kpis/vo/SqlStrVO'
import { toSqlStrVO } from '/@/model/cn/core/kpis/vo/SqlStrVO'

@Options({
  name: 'SqlStr',
  components: {
  },
  computed: {
    ...mapStores(useCoreToolStore, useSqlStrStore),
  },
})
export default class SqlStr extends Vue {

  sqlStrListDataSet: Array<SqlStrVO> = []

  sqlStrListColumns: Array<object> = [
    {
      title: '名称',
      dataIndex: 'sqlName',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '分类',
      dataIndex: 'sqlType',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: 'sql',
      dataIndex: 'sqlStr',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '描述',
      dataIndex: 'description',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '排序权重',
      dataIndex: 'weightOrder',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
  ]

  sqlStrListLoading: boolean = false

  sqlStrListSelectedRowKeys: string[] = []

  sqlStrListSelectData: Array<SqlStrVO> = []

  sqlStrListSelectLength: number = 0

  sqlStrListSelectCurrentRowIndex: number = -1

  sqlStrListSelectCurrentRowData: SqlStrVO = toSqlStrVO()

  sqlStrListPaginationVisible: boolean = true

  sqlStrListDataPage: number = 1

  sqlStrListDataRows: number = 10

  sqlStrListDataCount: number = 0

  sqlStrListRowClickTimeout: number | null = null

  sqlStrListRowOnChange(selectedRowKeys, selectedRows) {
    this.sqlStrListSelectedRowKeys = [ ...selectedRowKeys ]
    this.sqlStrListSelectData = [ ...selectedRows ]
    this.sqlStrListSelectLength = selectedRows.length
  }

  sqlStrListCustomRow(record, index) {
    return {
      'class': index === this.sqlStrListSelectCurrentRowIndex ? 'ant-table-row-selected' : '',
      onClick: (event) => {
        if (this.sqlStrListRowClickTimeout) {
          clearTimeout(this.sqlStrListRowClickTimeout)
          this.sqlStrListRowClickTimeout = null
        } else {
          this.sqlStrListRowClickTimeout = setTimeout(() => {
            this.sqlStrListRowClickTimeout = null
            this.sqlStrListSelectCurrentRowIndex = index
            this.sqlStrListSelectCurrentRowData = { ...record }
            let selectedRowKeys = [ ...this.sqlStrListSelectedRowKeys ]
            let selectedRows = [ ...this.sqlStrListSelectData ]
            if (selectedRowKeys.includes(record.id)) {
              this.sqlStrListRowOnChange([ ...selectedRowKeys.filter(eo => { return eo !== record.id }) ], [ ...selectedRows.filter(eo => { return eo.id !== record.id }) ])
            } else {
              selectedRowKeys.push(record.id)
              selectedRows.push(record)
              this.sqlStrListRowOnChange([ ...new Set(selectedRowKeys) ], [ ...new Set(selectedRows) ])
            }
          }, 200)
        }
      },
      onDblclick: (event) => {
        this.sqlStrStore.pkVo(this.$toParam<SqlStrQO>({ id: record.id } as SqlStrQO)).then((res: Res<SqlStrVO>) => {
          if (res.succ) {
            this.sqlStrDetailModel = { ...res.data[0] }
            this.sqlStrDetailDrawerVisible = true
          } else {
            this.$message.error(res.msg, 6)
          }
        })
      },
    }
  }

  sqlStrListGetDataSet() {
    this.sqlStrListLoading = true
    this.sqlStrListRowOnChange([], [])
    this.sqlStrListSelectCurrentRowIndex = -1
    this.sqlStrListSelectCurrentRowData = toSqlStrVO()
    this.sqlStrStore.pagingVo(this.$toParam<SqlStrQO>({ weightOrderAsc: 'y' } as SqlStrQO, this.sqlStrListDataPage, this.sqlStrListDataRows)).then((res: Res<SqlStrVO>) => {
      if (res.succ) {
        this.sqlStrListPaginationVisible = true
        this.sqlStrListDataSet = [ ...res.data ]
        this.sqlStrListDataCount = res.count
        setTimeout(() => { this.sqlStrListLoading = false }, 500)
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.sqlStrListLoading = false }, 500)
      }
    }).catch(err => {
      setTimeout(() => { this.sqlStrListLoading = false }, 500)
    })
  }

  sqlStrListPaginationChange() {
    this.sqlStrListGetDataSet()
  }

  sqlStrListPaginationShowSizeChange() {
    this.sqlStrListGetDataSet()
  }

  sqlStrFormQueryVisible: boolean = false

  sqlStrFormQuerySubmitting: boolean = false

  sqlStrFormQueryModel: SqlStrQO = toSqlStrQO()

  sqlStrFormQueryUpDown() {
    this.sqlStrFormQueryVisible = !this.sqlStrFormQueryVisible
  }

  sqlStrFormQueryReset() {
    if (this.$refs.sqlStrFormQueryRef) {
      this.$refs.sqlStrFormQueryRef.resetFields()
      this.sqlStrFormQueryModel = toSqlStrQO()
    }
  }

  sqlStrFormQuerySearch() {
    this.sqlStrFormQuerySubmitting = true
    this.sqlStrListLoading = true
    this.sqlStrListRowOnChange([], [])
    this.sqlStrListSelectCurrentRowIndex = -1
    this.sqlStrListSelectCurrentRowData = toSqlStrVO()
    this.sqlStrStore.queryVo(this.$toParam<SqlStrQO>(this.$toSearch({ ...this.sqlStrFormQueryModel }))).then((res: Res<SqlStrVO>) => {
      if (res.succ) {
        this.sqlStrListPaginationVisible = false
        this.sqlStrListDataSet = [ ...res.data ]
        this.sqlStrListDataCount = res.count
        setTimeout(() => { this.sqlStrFormQuerySubmitting = false }, 5000)
        setTimeout(() => { this.sqlStrListLoading = false }, 500)
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.sqlStrFormQuerySubmitting = false }, 5000)
        setTimeout(() => { this.sqlStrListLoading = false }, 500)
      }
    }).catch(err => {
      setTimeout(() => { this.sqlStrFormQuerySubmitting = false }, 5000)
      setTimeout(() => { this.sqlStrListLoading = false }, 500)
    })
  }

  sqlStrEditDrawerVisible: boolean = false

  sqlStrFormSaveSubmitting: boolean = false

  sqlStrFormSaveContinue: boolean = false

  sqlStrFormModel: SqlStr = toSqlStr()

  sqlStrFormRules: object = {
    // sqlName: [
    //   { type: 'string', required: true, message: '名称不能为空！', trigger: ['change', 'blur'] },
    // ],
    // sqlType: [
    //   { type: 'string', required: true, message: '分类不能为空！', trigger: ['change', 'blur'] },
    // ],
    // sqlStr: [
    //   { type: 'string', required: true, message: 'sql不能为空！', trigger: ['change', 'blur'] },
    // ],
    // description: [
    //   { type: 'string', required: true, message: '描述不能为空！', trigger: ['change', 'blur'] },
    // ],
    // weightOrder: [
    //   { type: 'number', required: true, message: '排序权重不能为空！', trigger: ['change', 'blur'] },
    // ],
  }

  sqlStrFormReset() {
    if (this.$refs.sqlStrFormRef) {
      this.$refs.sqlStrFormRef.resetFields()
      this.sqlStrFormModel = toSqlStr()
    }
  }

  sqlStrFormSubmit() {
    this.sqlStrFormSaveSubmitting = true
    this.$refs.sqlStrFormRef.validate().then(() => {
      if (this.sqlStrFormModel.id) {
        this.sqlStrStore.update(this.$toParam<SqlStr>(this.sqlStrFormModel)).then((res: Res<SqlStr>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.sqlStrFormReset()
            this.sqlStrFormSaveSubmitting = false
            if (!this.sqlStrFormSaveContinue) {
              this.sqlStrEditDrawerVisible = false
              this.sqlStrListGetDataSet()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.sqlStrFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.sqlStrFormSaveSubmitting = false }, 3000)
        })
      } else {
        this.sqlStrStore.save(this.$toParam<SqlStr>(this.sqlStrFormModel)).then((res: Res<SqlStr>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.sqlStrFormReset()
            this.sqlStrFormSaveSubmitting = false
            if (!this.sqlStrFormSaveContinue) {
              this.sqlStrEditDrawerVisible = false
              this.sqlStrListGetDataSet()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.sqlStrFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.sqlStrFormSaveSubmitting = false }, 3000)
        })
      }
    }).catch(() => {
      setTimeout(() => { this.sqlStrFormSaveSubmitting = false }, 3000)
    })
  }

  sqlStrFormSave() {
    this.sqlStrFormSaveContinue = false
    this.sqlStrFormSubmit()
  }

  sqlStrFormNextAdd() {
    this.sqlStrFormSaveContinue = true
    this.sqlStrFormSubmit()
  }

  sqlStrAdd() {
    if (this.sqlStrFormModel.id) {
      this.sqlStrFormReset()
    }
    this.sqlStrEditDrawerVisible = true
  }

  sqlStrUpdate() {
    this.sqlStrStore.pk(this.$toParam<SqlStrQO>({ id: this.sqlStrListSelectData[0].id } as SqlStrQO)).then((res: Res<SqlStr>) => {
      if (res.succ) {
        this.sqlStrFormReset()
        this.sqlStrFormModel = { ...res.data[0] }
        this.sqlStrEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  sqlStrEditDrawerOnClose() {
    if (this.sqlStrFormSaveContinue) {
      this.sqlStrFormSaveContinue = false
      this.sqlStrListGetDataSet()
    }
  }

  sqlStrDelete() {
    let param: Array<SqlStr> = []
    this.sqlStrListSelectData.forEach((eo: SqlStrVO) => {
      param.push({ id: eo.id } as SqlStr)
    })
    this.sqlStrStore.removeBatch(this.$toParam<SqlStr>(param)).then((res: Res<SqlStr>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
        this.sqlStrListGetDataSet()
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  sqlStrDetailDrawerVisible: boolean = false

  sqlStrDetailModel: SqlStrVO = toSqlStrVO()

  sqlStrDetailInfo() {
    this.sqlStrStore.pkVo(this.$toParam<SqlStrQO>({ id: this.sqlStrListSelectData[0].id } as SqlStrQO)).then((res: Res<SqlStrVO>) => {
      if (res.succ) {
        this.sqlStrDetailModel = { ...res.data[0] }
        this.sqlStrDetailDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  sqlStrDetailUpdate() {
    this.sqlStrStore.pk(this.$toParam<SqlStrQO>({ id: this.sqlStrDetailModel.id } as SqlStrQO)).then((res: Res<SqlStr>) => {
      if (res.succ) {
        this.sqlStrFormReset()
        this.sqlStrFormModel = { ...res.data[0] }
        this.sqlStrDetailDrawerVisible = false
        this.sqlStrEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  sqlStrTemplateDownload() {
    this.coreToolStore.getValueByConfKey('defaultDataImportTemplate').then((res: string) => {
      window.open(this.coreToolStore.getFileFullUrl(res), '_blank')
    })
  }

  sqlStrDataImportDrawerVisible: boolean = false

  sqlStrDataImportFileSelectDrawerVisible: boolean = false

  sqlStrDataImportFileModule: string = 'dataImport'

  sqlStrDataImportFileUrl: string = ''

  sqlStrDataImportSaveSubmitting: boolean = false

  sqlStrDataImport() {
    this.sqlStrDataImportDrawerVisible = true
  }

  sqlStrDataImportFileUploadChange(value) {
    this.sqlStrDataImportFileUrl = value
  }

  sqlStrDataImportFileUrlSelectFileClick() {
    this.sqlStrDataImportFileSelectDrawerVisible = true
  }

  sqlStrDataImportFileSelectChange(fileUrl) {
    this.sqlStrDataImportFileUrl = fileUrl
    this.sqlStrDataImportFileSelectDrawerVisible = false
  }

  sqlStrToDataImport() {
    this.sqlStrDataImportSaveSubmitting = true
    this.sqlStrStore.dataImport(this.$toParam<string>(this.sqlStrDataImportFileUrl)).then((res: Res<SqlStr>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
        this.$refs.sqlStrDataImportFileUploadRef.resetFiles()
        this.sqlStrDataImportFileUrl = ''
        this.sqlStrDataImportSaveSubmitting = false
        this.sqlStrDataImportDrawerVisible = false
        this.sqlStrListGetDataSet()
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.sqlStrDataImportSaveSubmitting = false }, 3000)
      }
    }).catch(err => {
      setTimeout(() => { this.sqlStrDataImportSaveSubmitting = false }, 3000)
    })
  }

  sqlStrDataExportDrawerVisible: boolean = false

  sqlStrOptionalDataExportFieldList: Array<DataExportFieldAO> = [
    {
      fieldName: 'sqlName',
      fieldComment: '名称',
    },
    {
      fieldName: 'sqlType',
      fieldComment: '分类',
    },
    {
      fieldName: 'sqlStr',
      fieldComment: 'sql',
    },
    {
      fieldName: 'description',
      fieldComment: '描述',
    },
    {
      fieldName: 'weightOrder',
      fieldComment: '排序权重',
    },
  ]

  sqlStrSelectedDataExportFieldList: Array<DataExportFieldAO> = []

  sqlStrDataExport() {
    this.sqlStrDataExportDrawerVisible = true
  }

  sqlStrToDataExport(allData: boolean) {
    this.sqlStrStore.dataExport(this.$toParam<DataExportAO<SqlStrQO>>(toDataExportAO(this.sqlStrSelectedDataExportFieldList, allData ? { weightOrderAsc: 'y' } as SqlStrQO : this.$toSearch({ ...this.sqlStrFormQueryModel }), 'sql语句信息数据导出.xlsx'))).then((res: Res<string>) => {
      if (res.succ) {
        window.open(this.coreToolStore.getFileFullUrl(res.data[0]), '_blank')
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  mounted() {
    this.sqlStrListGetDataSet()
  }

}
</script>

<style scoped lang="less">

</style>
